package com.tanhua.dubbo.api;

import com.mongodb.client.result.UpdateResult;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class CommentApiImpl implements CommentApi{
    @Autowired
    private MongoTemplate mongoTemplate;

    //根据动态id查询评论信息并分页
    @Override
    public List<Comment> findPagesByMovementId(String movementId, CommentType comment, Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(movementId))
                                            .and("commentType").is(comment.getType()));
        query.skip((page-1)*pagesize)
                .limit(pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;
    }

    //通用的互动数据保存
    @Override
    public Integer save(Comment comment) {
        //-----------Comment互动数据操作：新增------------------
        //根据动态id查询出动态信息
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        if (movement == null){
            throw new RuntimeException("动态不存在,无法进行评论点赞喜欢");
        }
        //设置被评论人的id
        comment.setPublishUserId(movement.getUserId());
        mongoTemplate.save(comment);
        //-----------Movement动态数据操作：互动数据进行+1操作------------------
        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));

        //设置修改的数值 根据commentType来判断,对应的互动数量加1
        Update update = new Update();
        if (comment.getCommentType() ==1){
            update.inc("likeCount",1);
        }else if (comment.getCommentType() == 2) {
            update.inc("commentCount",1);
        }else if (comment.getCommentType() == 3) {
            update.inc("loveCount",1);
        }

        //设置修改参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);//returnNew设置是否返回修改后的值

        //使用find and modify方法,实现修改并返回结果
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        return modify.statisCount(comment.getCommentType());
    }

    //查询数据是否存在
    @Override
    public Boolean hasComment(String movementId, Long userId, CommentType type) {
        Query query = Query.query(Criteria.where("publishId").is(new ObjectId(movementId))
                        .and("userId").is(userId)
                        .and("commentType").is(type.getType()));
        return mongoTemplate.exists(query,Comment.class);
    }

    //通用数据删除
    @Override
    public Integer remove(Comment comment) {
        //-----------Comment互动数据操作：删除------------------
        //删除comment表中的数据
        Query queryComment = Query.query(Criteria.where("publishId").is(comment.getPublishId())
                .and("userId").is(comment.getUserId())
                .and("commentType").is(comment.getCommentType()));
        mongoTemplate.remove(queryComment,Comment.class);

        //-----------Movement动态数据操作：互动数据进行-1操作------------------
        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));

        //设置修改的数值 根据commentType来判断,对应的互动数量加1
        Update update = new Update();
        if (comment.getCommentType() ==1){
            update.inc("likeCount",-1);
        }else if (comment.getCommentType() == 2) {
            update.inc("commentCount",-1);
        }else if (comment.getCommentType() == 3) {
            update.inc("loveCount",-1);
        }

        //设置修改参数
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);//returnNew设置是否返回修改后的值

        //使用find and modify方法,实现修改并返回结果
        Movement modify = mongoTemplate.findAndModify(query, update, options, Movement.class);
        return modify.statisCount(comment.getCommentType());
    }

    @Override
    public Comment findById(String id) {
        ObjectId objectId = new ObjectId(id);
        Comment comment = mongoTemplate.findById(objectId, Comment.class);
        return comment;
    }

    //评论点赞
    @Override
    public Integer commentLike(Comment comment) {

        Query query = Query.query(Criteria.where("id").is(comment.getId()));
        Update update = new Update();
        update.inc("likeCount",1);

        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);

        comment = mongoTemplate.findAndModify(query, update, options, Comment.class);
        return comment.getLikeCount();
    }

    //取消点赞
    @Override
    public Integer commentDislike(Comment comment) {
        Query query = Query.query(Criteria.where("id").is(comment.getId()));
        Update update = new Update();
        update.inc("likeCount",-1);

        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);

        comment = mongoTemplate.findAndModify(query, update, options, Comment.class);
        return comment.getLikeCount();
    }

    //视频通用保存
    @Override
    public void videoSave(Comment comment) {
        //-----------Comment互动数据操作：新增------------------
        //根据动态id查询出视频信息
        Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);
        if (video == null){
            throw new RuntimeException("视频不存在,无法进行评论点赞喜欢");
        }
        //设置被评论人的id
        comment.setPublishUserId(video.getUserId());
        mongoTemplate.save(comment);
        //-----------Movement动态数据操作：互动数据进行+1操作------------------
        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));

        //设置修改的数值 根据commentType来判断,对应的互动数量加1
        Update update = new Update();
        if (comment.getCommentType() ==1){
            update.inc("likeCount",1);
        }else if (comment.getCommentType() == 2) {
            update.inc("commentCount",1);
        }else if (comment.getCommentType() == 3) {
            update.inc("loveCount",1);
        }

        mongoTemplate.updateFirst(query,update,Video.class);
    }

    //视频通用删除
    @Override
    public void VideoRemove(Comment comment) {
        //-----------Comment互动数据操作：删除------------------
        //删除comment表中的数据
        Query queryComment = Query.query(Criteria.where("publishId").is(comment.getPublishId())
                .and("userId").is(comment.getUserId())
                .and("commentType").is(comment.getCommentType()));
        mongoTemplate.remove(queryComment,Comment.class);

        //-----------Movement动态数据操作：互动数据进行-1操作------------------
        Query query = Query.query(Criteria.where("id").is(comment.getPublishId()));

        //设置修改的数值 根据commentType来判断,对应的互动数量加1
        Update update = new Update();
        if (comment.getCommentType() ==1){
            update.inc("likeCount",-1);
        }else if (comment.getCommentType() == 2) {
            update.inc("commentCount",-1);
        }else if (comment.getCommentType() == 3) {
            update.inc("loveCount",-1);
        }
        mongoTemplate.updateFirst(query,update,Video.class);
    }
}
